/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.thu.laud.objectstore.index;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.cassandra.utils.ByteBufferUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author zhuoan
 *
 */
public final class ByteArray implements Serializable
{
	private static final Logger log = LoggerFactory.getLogger(ByteArray.class);
	private final byte[] data;

    public ByteArray(byte[] data)
    {
        if (data == null)
        {
          log.error("data array received null");
          throw new NullPointerException();
        }
        this.data = data;
    }
    
    public ByteBuffer getByteBuffer(){
    	
    	return ByteBuffer.wrap(this.data);
    }
    
    public byte[] getData(){
    	
    	return this.data;
    }
    
    public static ByteArray getByteArray(ByteBuffer data){
    	
    	return new ByteArray(ByteBufferUtil.getArray(data));
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(data);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof ByteArray)) {
			return false;
		}
		ByteArray other = (ByteArray) obj;
		if (!Arrays.equals(data, other.data)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return new String(data);
	}
	
	

    
}
